﻿@{
    string LinkText = ViewBag.LinkText;
    LinkText = System.Text.RegularExpressions.Regex.Replace(LinkText, "(\\?|&)p=[\\d]*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    if (LinkText.Contains("?"))
    {
        LinkText += "&p=";
    }
    else
    {
        LinkText += "?p=";
    }

    //int PageSize = ViewBag.PageSize;
    int CurrentPageNumber = ViewBag.PageIndex;
    int numPage = ViewBag.NumPage;
}

@if (numPage > 1)
{
        <ul class="pagination pagination-sm">
            @if (CurrentPageNumber > 1)
            {
                <li><a href="@(LinkText + 1)">First</a></li>
                <li><a href="@(LinkText + (CurrentPageNumber - 1))">Prev</a></li>
            }

            @if (CurrentPageNumber > 4)
            {
                <li><a href="@(LinkText + (CurrentPageNumber - 1))">...</a></li>
            }

            @if (CurrentPageNumber <= 4)
            {
                for (int i = 1; i <= 7 && i <= numPage; i++)
                {
                    if (i == CurrentPageNumber)
                    {
                        <li class="active"><a href="@(LinkText + i)">@i</a></li>
                    }
                    else
                    {
                        <li><a href="@(LinkText + i)">@i</a></li>
                    }
                }
            }
            else if (CurrentPageNumber >= numPage - 3)
            {
                for (int i = numPage > 6 ? numPage - 6 : 1; i <= numPage; i++)
                {
                    if (i == CurrentPageNumber)
                    {
                        <li class="active"><a href="@(LinkText + i)">@i</a></li>
                    }
                    else
                    {
                        <li><a href="@(LinkText + i)">@i</a></li>
                    }
                }
            }
            else
            {
                for (int i = CurrentPageNumber - 3; i <= CurrentPageNumber + 3; i++)
                {
                    if (i == CurrentPageNumber)
                    {
                        <li class="active"><a href="@(LinkText + i)">@i</a></li>
                    }
                    else
                    {
                        <li><a href="@(LinkText + i)">@i</a></li>
                    }
                }
            }

            @if (CurrentPageNumber < numPage - 3)
            {
                <li><a href="@(LinkText + (CurrentPageNumber + 1))">...</a></li>
            }

            @if (CurrentPageNumber < numPage)
            {
                <li><a href="@(LinkText + (CurrentPageNumber + 1))">Next</a></li>
                <li><a href="@(LinkText + numPage)">Last</a></li>
            }
        </ul>
}